/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogNouveau.java
 *
 * Created on 27 avr. 2011, 19:53:21
 */

package Vue;

import javax.swing.JOptionPane;

/**
 * Classe permettant de créer une fenêtre de dialogue pour choisir la taille d'une nouvelle grille
 * @author FERRIER/NONGLATON
 */
public class DialogNouveau extends javax.swing.JDialog {

    private boolean succes = false;

    /** Créé une nouvelle fenêtre de dialogue pour la création d'une nouvelle grille
     * @param parent la fenêtre parent
     * @param modal true si la fenêtre doit être modale, false sinon
     */
    public DialogNouveau(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setMaximumSize(this.getSize());
        this.setMinimumSize(this.getSize());
        this.setLocationRelativeTo(this.getParent());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        boutonOK = new javax.swing.JButton();
        boutonAnnuler = new javax.swing.JButton();
        pnlDimensions = new javax.swing.JPanel();
        lblHauteur = new javax.swing.JLabel();
        lblLargeur = new javax.swing.JLabel();
        txtFieldHauteur = new javax.swing.JTextField();
        txtFieldLargeur = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Nouveau...");

        boutonOK.setText("OK");
        boutonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                actionOk(evt);
            }
        });

        boutonAnnuler.setText("Annuler");
        boutonAnnuler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                actionAnnuler(evt);
            }
        });

        pnlDimensions.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Dimensions"));
        pnlDimensions.setToolTipText("Saisissez les valeurs de la nouvelle grille");
        pnlDimensions.setName(""); // NOI18N
        pnlDimensions.setRequestFocusEnabled(false);
        pnlDimensions.setVerifyInputWhenFocusTarget(false);
        pnlDimensions.setLayout(new java.awt.GridBagLayout());

        lblHauteur.setText("Hauteur:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnlDimensions.add(lblHauteur, gridBagConstraints);

        lblLargeur.setText("Largeur:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnlDimensions.add(lblLargeur, gridBagConstraints);

        txtFieldHauteur.setPreferredSize(new java.awt.Dimension(35, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnlDimensions.add(txtFieldHauteur, gridBagConstraints);

        txtFieldLargeur.setPreferredSize(new java.awt.Dimension(35, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnlDimensions.add(txtFieldLargeur, gridBagConstraints);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlDimensions, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(boutonOK, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(boutonAnnuler, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlDimensions, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(boutonAnnuler)
                    .addComponent(boutonOK))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void actionAnnuler(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actionAnnuler
        dispose();
}//GEN-LAST:event_actionAnnuler

    private void actionOk(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actionOk
        if(checkField()){
            succes = true;
            dispose();
        }else{
            JOptionPane.showMessageDialog(this,
                    "Veuillez remplir correctement les champs",
                    "Erreur",
                    JOptionPane.ERROR_MESSAGE);

        }
    }//GEN-LAST:event_actionOk

    /**
    * @param args the command line arguments
    */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton boutonAnnuler;
    private javax.swing.JButton boutonOK;
    private javax.swing.JLabel lblHauteur;
    private javax.swing.JLabel lblLargeur;
    private javax.swing.JPanel pnlDimensions;
    private javax.swing.JTextField txtFieldHauteur;
    private javax.swing.JTextField txtFieldLargeur;
    // End of variables declaration//GEN-END:variables

    /**
     * Vérifie si les champs sont remplis correctement
     * @return true si les champs sont bien remplis, false si ils sont mal remplis
     */
    private boolean checkField() {
        try {
            Integer.valueOf(txtFieldHauteur.getText());
            Integer.valueOf(txtFieldLargeur.getText());
        } catch (NumberFormatException ex) {
            return false;
        }
        return true;
    }

    /**
     * Retourne si l'utilisateur a validé ou annulé
     * @return true si l'utilisateur a validé et false si il a annulé
     */
    public boolean reussis(){
        return succes;
    }

    /**
     * Retourne la hauteur saisie par l'utilisateur
     * @return hauteur 
     */
    public int getHauteur(){
        return Integer.valueOf(txtFieldHauteur.getText());
    }

    /**
     * Retourne la largeur saisie par l'utilisateur
     * @return largeur
     */
    public int getLargeur(){
        return Integer.valueOf(txtFieldLargeur.getText());
    }


}
